package main

import (
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"

	"github.com/mojocn/base64Captcha"
)

func say() {
	fmt.Println("hello")

}

//ConfigJsonBody json request body.
type ConfigJsonBody struct {
	Id              string
	CaptchaType     string
	VerifyValue     string
	ConfigAudio     base64Captcha.ConfigAudio
	ConfigCharacter base64Captcha.ConfigCharacter
	ConfigDigit     base64Captcha.ConfigDigit
}

func outputimage(w http.ResponseWriter, r *http.Request) {

	body, _ := ioutil.ReadFile("yang.png")
	fmt.Println(len(body))
	w.Header().Set("Content-Type", "image/apng")
	fmt.Println(w.Write(body))
	b := base64.StdEncoding.EncodeToString(body)
	fmt.Println(len(b))
}

// base64Captcha create http handler
func generateCaptchaHandler(w http.ResponseWriter, r *http.Request) {
	//parse request parameters
	//接收客户端发送来的请求参数
	// decoder := json.NewDecoder(r.Body)
	// var postParameters ConfigJsonBody
	// err := decoder.Decode(&postParameters)
	// if err != nil {
	// 	log.Println(err)
	// }
	// defer r.Body.Close()

	// //create base64 encoding captcha
	// //创建base64图像验证码

	// var config interface{}
	// switch postParameters.CaptchaType {
	// case "audio":
	// 	config = postParameters.ConfigAudio
	// case "character":
	// 	config = postParameters.ConfigCharacter
	// default:
	// 	config = postParameters.ConfigDigit
	// }

	//GenerateCaptcha 第一个参数为空字符串,包会自动在服务器一个随机种子给你产生随机uiid.
	config := base64Captcha.ConfigDigit{}
	config.CaptchaLen = 6
	config.DotCount = 80
	config.Height = 80
	config.MaxSkew = 0.7
	config.Width = 240

	captchaId, digitCap := base64Captcha.GenerateCaptcha("123456", config)
	base64Png := base64Captcha.CaptchaWriteToBase64Encoding(digitCap)

	// base64Captcha.CaptchaWriteToFile(digitCap, "./", "yang", "png")

	//or you can do this
	//你也可以是用默认参数 生成图像验证码
	//base64Png := captcha.GenerateCaptchaPngBase64StringDefault(captchaId)

	//set json response
	//设置json响应

	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	body := map[string]interface{}{"code": 1, "data": base64Png, "captchaId": captchaId, "msg": "success"}
	json.NewEncoder(w).Encode(body)

}

// base64Captcha verify http handler
func captchaVerifyHandle(w http.ResponseWriter, r *http.Request) {

	//parse request parameters
	//接收客户端发送来的请求参数
	decoder := json.NewDecoder(r.Body)
	var postParameters ConfigJsonBody
	err := decoder.Decode(&postParameters)
	if err != nil {
		log.Println(err)
	}
	defer r.Body.Close()
	//verify the captcha
	//比较图像验证码
	verifyResult := base64Captcha.VerifyCaptcha("123456", postParameters.VerifyValue)

	//set json response
	//设置json响应
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	body := map[string]interface{}{"code": "error", "data": "验证失败", "msg": "captcha failed"}
	if verifyResult {
		body = map[string]interface{}{"code": "success", "data": "验证通过", "msg": "captcha verified"}
	}
	json.NewEncoder(w).Encode(body)
}

//start a net/http server
//启动golang net/http 服务器
func main() {

	//serve Vuejs+ElementUI+Axios Web Application
	http.Handle("/", http.FileServer(http.Dir("./")))

	//api for create captcha
	//创建图像验证码api
	http.HandleFunc("/api/getCaptcha", generateCaptchaHandler)

	//api for verify captcha
	http.HandleFunc("/api/verifyCaptcha", captchaVerifyHandle)
	http.HandleFunc("/image", outputimage)

	fmt.Println("Server is at localhost:3333")
	if err := http.ListenAndServe("localhost:3333", nil); err != nil {
		log.Fatal(err)
	}
}
